package com.cjst.thread;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.cjst.pojo.thirdparty.JPush;
import com.cjst.pojo.thirdparty.JPushInfo;
import com.cjst.service.thirdparty.JPushInfoService;
import com.cjst.service.thirdparty.JPushService;
import com.cjst.thirdparty.jpush.JPushClientUtil;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;


//@Component
public class JPushThread implements Runnable {
	
	@Autowired
	private static JPushService jpushService;
	@Autowired
	private static JPushInfoService jpushInfoService;
	
	private Platform platform=null;
	private Audience audience=null;
	private Notification notification=null;
	private Message Message=null;
	private Options Options=null;
	
	private PushPayload payload=null;

	

	@Override
	public void run() {
		// TODO Auto-generated method stub

		try {
			if (payload == null) {
				if (platform == null || audience == null || notification == null) {
					return;
				}
				payload = PushPayload.newBuilder()
						.setPlatform(platform)
						.setAudience(audience)
						.setNotification(notification)
						.setMessage(Message)
						.setOptions(Options)
						.build();
			}
			
			JPush jpush=new JPush(payload);
			String jpushId=jpushService.addLocalByid(jpush);
			
			JPushInfo jpushInfo=new JPushInfo();
			jpushInfo.setJpushId(jpushId);
			String id=jpushInfoService.addLocalByid(jpushInfo);

			JPushClientUtil.getjPushClient().sendPush(payload);

			
			
		} catch (APIConnectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (APIRequestException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}



	public Platform getPlatform() {
		return platform;
	}



	public void setPlatform(Platform platform) {
		this.platform = platform;
	}



	public Audience getAudience() {
		return audience;
	}



	public void setAudience(Audience audience) {
		this.audience = audience;
	}



	public Notification getNotification() {
		return notification;
	}



	public void setNotification(Notification notification) {
		this.notification = notification;
	}



	public Message getMessage() {
		return Message;
	}



	public void setMessage(Message message) {
		Message = message;
	}



	public Options getOptions() {
		return Options;
	}



	public void setOptions(Options options) {
		Options = options;
	}



	public PushPayload getPayload() {
		return payload;
	}



	public void setPayload(PushPayload payload) {
		this.payload = payload;
	}
	
	

}
